#ifndef PAGE3_H
#define PAGE3_H

#include <QWidget>
#include <QFileDialog>
#include <QString>
#include <cv.h>
#include <highgui.h>
#include <list>
#include <stdio.h>
#include <opencv.hpp>
using namespace cv;
using namespace std;

namespace Ui {
class Page3;
}

class Page3 : public QWidget
{
    Q_OBJECT

public:
    explicit Page3(QWidget *parent = nullptr);
    ~Page3();
    void bgModeling(const char * videoFilePath, const int frame_num_used, IplImage ** bgImg, \
        const int size1 = 5, const int size2 = 5, const int sigma1 = 4, const int sigma2 = 4);
    struct block {
        //functions
        block(CvRect rect, int t, IplImage * img); //构造函数
        ~block(); //析构函数

                 //variables
        CvRect position; //团块在源图中位置
        int t_sec; //所在帧时间
        IplImage * target; //图像

    };
    void buildTrackDB(const char * videoFilePath, const IplImage * bgImg, list< list<block *> > & database, int &fps, CvSize &size, bool save_mode,\
        const int threshold = 30, const int min_area = 3000, const int obj_num = 3, \
        const float extend_factor = 0.2, const float category_factor = 0.5);
    void mergeDB(list< list<block *> > & database, const char * videoFilePath, const int fps, const CvSize size, const IplImage * bgImg);
    void freeDB(list< list<block *> > & database);
    void videoproce();
    bool isSameObj(const CvRect & a, const CvRect & b, const float category_factor);
    void mergeRects(list<CvRect> & rects);
    bool isOverlap(const CvRect & a, const CvRect & b);
    void getFgImage(const IplImage * frame, IplImage * fgImg, const IplImage * bgImg, const int threshold);
    QImage cvMat2QImage(const cv::Mat& mat);

private slots:
    void on_pushButton_3_clicked();

    void on_pushButton_clicked();
    
    void on_pushButton_2_clicked();

    void getVideoPath(QString);

    void getVideoTime(qint64);

private:
    Ui::Page3 *ui;
    QString videoOutPath=NULL;
    QString videoPath=NULL;
    bool save_mode;
    qint64 playTimes;

signals:
    void sendNewStr(QString);
};

#endif // PAGE3_H
